﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Text;
using ScrumTable.Common.Text;

#endregion

namespace ScrumTable.Common.Collections
{
    /// <summary>
    /// Parses a ValueChain and divides the results into ValueChain and
    /// MultiValueChain instances.
    /// </summary>
    public static class ValueChainSerializer
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Parses the given string and returns the created chain.
        /// </summary>
        /// <param name="input">Input to parse.</param>
        /// <returns>Returns the new value chain.</returns>
        /// <remarks>
        /// A chain is formated as follows:
        ///  Id1 # Id2 # Id2
        ///   OR
        ///  Id1.1 # Id1.2 # Id1.2, Id2.1 # Id2.2 # Id2.2
        /// </remarks>
        public static ValueChain<string> Parse(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return ValueChain<string>.Empty;
            }
            if (input.Contains(MultiValueChain<string>.ChainSeparator[0]))
            {
                IList<ValueChain<string>> chains = new List<ValueChain<string>>();

                input.Split(
                    MultiValueChain<string>.ChainSeparator,
                    StringSplitOptions.RemoveEmptyEntries)
                        .ForEach(c => chains.Add( ToValueChain(c)));

                return new MultiValueChain<string>(chains);
            }
            return ToValueChain(input);
        }


        public static string Serialize<T>(ValueChain<T> toSerialize)
            where T : class
        {
            if (toSerialize == null)
            {
                return string.Empty;
            }
            return toSerialize.Serialize();
        }

        private static ValueChain<string> ToValueChain(string toParse)
        {
            return new ValueChain<string>(toParse.Split(ValueChain<string>.ValueSeparator, StringSplitOptions.RemoveEmptyEntries));
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}